package 剑指offer;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @ClassName _50第一个只出现一次的字符
 * @Description TODO
 * @Author 昝亚杰
 * @Date 2022/3/1 20:04
 * Version 1.0
 **/
public class _50第一个只出现一次的字符 {
    public char firstUniqChar(String s) {//无序hashmap
        char[] c = s.toCharArray();
        Map<Character,Boolean> map = new HashMap<>();
        for(int i = 0; i < c.length; i++){
            map.put(c[i],!map.containsKey(c[i]));
        }
        for(int i = 0; i < c.length; i++){
            if(map.get(c[i])){
                return c[i];
            }
        }
        return ' ';
    }
 /*   public char firstUniqChar(String s) {//有序的linkedhashmap，字符串很长时会比方法一减少遍历次数
        Map<Character, Boolean> dic = new LinkedHashMap<>();
        char[] sc = s.toCharArray();
        for(char c : sc)
            dic.put(c, !dic.containsKey(c));
        for(Map.Entry<Character, Boolean> d : dic.entrySet()){
            if(d.getValue()) return d.getKey();
        }
        return ' ';
    }*/

}
